package com.weseal.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author weixiaoqing
 * @description
 * @create 2017/9/6
 * @company 上海富捷信息技术有限公司
 */
//@Configuration
//@MapperScan(basePackages = "com.weseal.dao")
//@PropertySource("classpath:jdbc.properties")
//@EnableTransactionManagement
public class DataSourceConfig {
	private static final Logger logger = Logger.getLogger(DataSourceConfig.class);

	@Autowired
	private Environment env;


	@Bean
	public DataSource dataSource() {

		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(env.getProperty("jdbc.driver"));
		dataSource.setUrl(env.getProperty("jdbc.url"));
		dataSource.setUsername(env.getProperty("jdbc.username"));
		dataSource.setPassword(env.getProperty("jdbc.password"));
		//连接超时时间
		dataSource.setMaxWait(10000);
		//最大存活时间
		dataSource.setMaxActive(10000);
		// 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
		dataSource.setTimeBetweenEvictionRunsMillis(60000);
		//连接池中的最小生存时间
		dataSource.setMinEvictableIdleTimeMillis(300000);
		//这里建议配置为TRUE，防止取到的连接不可用
		dataSource.setTestOnBorrow(true);
		dataSource.setTestOnReturn(false);
		//自动提交 druidDataSource.setDefaultAutoCommit(true);
		dataSource.setPoolPreparedStatements(true);
		dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
		return dataSource;
	}

	@Bean
	@DependsOn("dataSource")
	public DataSourceTransactionManager dataSourceTransactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}

	@Bean
	@DependsOn("dataSourceTransactionManager")
	public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sessionFactory.setMapperLocations(resolver.getResources("classpath:com/weseal/dao/*.xml"));

		PathMatchingResourcePatternResolver configResolver = new PathMatchingResourcePatternResolver();
		sessionFactory.setConfigLocation(configResolver.getResource("classpath:com/weseal/mybatis/mybatisConfig.xml"));

		//分页控件
		PageInterceptor pageInterceptor = new PageInterceptor();
		Properties properties = new Properties();
		properties.setProperty("helperDialect", "mysql");
		properties.setProperty("supportMethodsArguments", "true");
		properties.setProperty("pageSizeZero", "true");
		pageInterceptor.setProperties(properties);
		sessionFactory.setPlugins(new Interceptor[]{pageInterceptor});
		sessionFactory.setDataSource(dataSource());
		return sessionFactory;
	}


}
